var that=this
import SidebarPopup from '../common/sidebar.js'; // 导入侧边栏入口
export default class ClickIndex {
	static instance = null; // 静态属性保存单例
	constructor() {
		if (ClickIndex.instance) return ClickIndex.instance;
		ClickIndex.instance = this;
		that=this
		this.homeState='init'
		this.ComSidebar = new SidebarPopup(); // 组件
		return ClickIndex.instance;
	}

	// 触摸事件处理
	handleTouch(e) {
		console.log("?????",that.homeState)
		const touch = e.touches[0];
		const x = touch.clientX;
		const y = touch.clientY;
		if(that.homeState=='init'){
			that.checkClick(x, y);
		}else{
			that.ComSidebar.checkClick(x, y);
		}
	}

	// 首页点击
	checkClick(x, y) {
		let canvas = GameGlobal.databus.canvas
		let btn1Bg = GameGlobal.databus.image.btn1Bg
		let rukouBg = GameGlobal.databus.image.rukouBg
		let ctx = GameGlobal.databus.ctx
		const kaishibtn = {
			x: canvas.width / 2 - btn1Bg.box.x,
			y: canvas.height / 2 - btn1Bg.box.y,
			w: btn1Bg.box.w,
			h: btn1Bg.box.h
		}
		// 点击开始游戏
		if (this.isInRect(x, y, kaishibtn)) {
			if (GameGlobal.databus.gamethis) {
				
				

				GameGlobal.databus.homethis.notrender()
				GameGlobal.databus.gamethis.render()
			} else {
				GameGlobal.databus.homethis.notrender()
				const loadTask = tt.loadSubpackage({
					name: 'stage1', // 填写 game.json 中 subPackages 填写的分包名称 name
					success: function(res) {},
					fail: function(res) {
						console.log("加载失败", ctx)
					}
				});
				loadTask.onProgressUpdate(res => {
					// console.log('下载进度', res.progress);
					// console.log('已经下载的数据长度', res.totalBytesWritten);
					// console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite);
				});
			}
		} else
			// 点击入口
			if (this.isInRect(x, y, rukouBg.box)) {
				this.homeState='side'
				this.ComSidebar.handleTap(ctx)
				
			}
	}


	// 辅助函数
	isInRect(x, y, rect) {
		return x >= rect.x && x <= rect.x + rect.w &&
			y >= rect.y && y <= rect.y + rect.h;
	}

	isInCircle(x, y, circle) {
		const dx = x - circle.x;
		const dy = y - circle.y;
		return dx * dx + dy * dy <= circle.r * circle.r;
	}

}


// export default {
// }